package com.gqhmt.auth.modules.sys.web.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.gqhmt.auth.modules.sys.dao.UserDao;
import com.gqhmt.auth.modules.sys.entity.User;
import com.gqhmt.auth.modules.sys.service.SystemService;
import com.gqhmt.auth.modules.sys.utils.UserUtils;

/**
 * 系统管理，安全相关实体的管理类,包括用户、角色、菜单.
 */
@RestController
@RequestMapping(value = "/api/rest")
public class ApiController {
	
	@Autowired
	private UserDao userDao;
	
	/**
	 * 接口测试
	 * @return
	 */
	@RequestMapping(value="/test", method=RequestMethod.GET)  
	@ResponseStatus(HttpStatus.OK)  
	public String getUserHtml() {  
		return "接口测试成功！";  
	}
	
	/**
	 * 根据登录名获取用户信息
	 * @param loginName
	 * @return
	 */
	@RequestMapping(value = "/userInfo", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)  
	public User getUserByLoginName(@RequestParam(value="loginName") String loginName,
			@RequestParam(value="systemName") String systemName) {
		User user = UserUtils.getUserByLoginName(loginName, systemName);
		return user;
	}
	
	/**
	 * 根据登录名获取用户信息
	 * @param loginName
	 * @return
	 */
	@RequestMapping(value = "/userPwdModify", method = RequestMethod.POST)
    @ResponseStatus(HttpStatus.OK)  
	public String userPwdModify(@RequestParam(value="loginName") String loginName,
			@RequestParam(value="oldPwd") String oldPwd,
			@RequestParam(value="newPwd") String newPwd) {
		System.out.println("用户：" + loginName + "接口修改密码！");
		User user = UserUtils.getUserByLoginName(loginName,"");
		if(null == user) {
			return "NoUser";
		}
		if(!SystemService.entryptPassword(oldPwd).equals(user.getPassword())) {
			return "oldPwdWrong";
		}
		user.setPassword(SystemService.entryptPassword(newPwd));
		userDao.updatePasswordById(user);
		return "OK";
	}
	
}
